package com.littora.alarm.config;

import java.io.*;
import java.util.Properties;

public class ConfigProperties {
    private final static String configName = "config.properties";
    private Properties properties = null;

    public ConfigProperties() throws FileNotFoundException {
        properties = new Properties();
        String currentDir = System.getProperty("user.dir");
        String propertyConfigName = currentDir +File.separator+ configName;
        File file = new File(propertyConfigName);
        FileInputStream fis = null;
        if(file.exists()) {
            fis = new FileInputStream(file);
        }
        // 使用ClassLoader加载properties配置文件生成对应的输入流
        InputStream in;
        if(fis == null) {
            in = ConfigProperties.class.getClassLoader().getResourceAsStream(configName);
        }else {
            in = fis;
        }
        // 使用properties对象加载输入流
        try {
            properties.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public Properties getProperties() {
        return properties;
    }

    public String getFileDir() {
        return properties.getProperty("alarm.file.dir");
    }

    public String getFileKeywork() {
        return properties.getProperty("alarm.file.keywork");
    }

    public String getEmailToken() {
        return properties.getProperty("alarm.email.token");
    }

    public String getEmailFrom() {
        return properties.getProperty("alarm.email.from");
    }

    public String getEmailTo() {
        return properties.getProperty("alarm.email.to");
    }

    public static void main(String[] args) throws FileNotFoundException {
        ConfigProperties configProperties = new ConfigProperties();
        System.err.println(configProperties.getEmailFrom());
    }
}
